package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.kuiandjie.entity.EmployeeProject;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class EmployeeProjectDao {
	private DBHelp<EmployeeProject> db = new DBHelp<EmployeeProject>();
	
	private class EmployeeProjectMap implements RowMapper<EmployeeProject>{

		@Override
		public EmployeeProject mapperRow(ResultSet rs) throws SQLException {
			EmployeeProject ep = new EmployeeProject();
			
			ep.setEmployeeId(rs.getInt("employee_id"));
			ep.setProjectId(rs.getInt("project_id"));
			ep.setRole(rs.getString("role"));
			
			return ep;
		}
	}

	public void insertEmplPro(int proId, int id, String role) {
		String sql = "INSERT INTO t_project_employee (t_project_id, t_employee_id, role) VALUES(?, ?, ?)";
		db.executeSQL(sql, proId, id, role);
	}
	
	public void insertEmplProMeber(int proId, String[] memberIds) {
		String str = "INSERT INTO t_project_employee (t_project_id, t_employee_id, role) VALUES(" + proId + "," + memberIds[0] + ",'member')";
		StringBuffer sb = new StringBuffer(str);
		
		for(int i = 1; i < memberIds.length; i++) {
			sb.append(",(");
			sb.append(proId);
			sb.append(",");
			sb.append(memberIds[i]);
			sb.append(",");
			sb.append("'member')");
		}
		
		String sql = sb.toString();
		db.executeSQL(sql);
	}

	public EmployeeProject findByEmpIdAndProId(int empId, String curpid) {
		String sql = "SELECT t_employee_id AS employee_id, t_project_id AS project_id, role FROM t_project_employee WHERE t_employee_id = ? AND t_project_id = ?";
		return db.executeQueryForObject(sql, new EmployeeProjectMap(), empId, curpid);
	}

	

	/*public EmployeeProject findByTaskIdAndEmpId(int curTaskId, int nameId) {
		String sql = "SELECT a.t_employee_id AS employee_id, a.t_project_id AS project_id, role FROM t_project_employee AS a, t_goal AS b, t_task AS c WHERE c.id = ? AND a.t_employee_id = ? AND b.id = c.t_goal_id AND b.t_project_id = a.t_project_id";
		return db.executeQueryForObject(sql, new EmployeeProjectMap(), curTaskId, nameId);
	}*/
}
